package com.zhanglijie.theory.readwrite;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/8/14 0014 16:18
 */
public class WriteThread extends Thread{
    private final TextData textData;
    private final String fileStr;
    private int index;
    public WriteThread(String name,TextData textData,String fileStr){
        super(name);
        this.textData = textData;
        this.fileStr = fileStr;
    }

    @Override
    public void run() {
        while (true){
            char c;
            try{
                c = nextChar();
            }catch (Exception e){
                //写完了线程就退出
                break;
            }
            //不然接着写
            try {
            textData.write(c);
                Thread.sleep(50);
            }catch (Exception e){}
        }
    }
    private char nextChar(){
        char c = fileStr.charAt(index);
        index++;
        if(index >= fileStr.length()){
            //throw new RuntimeException("读完了");
            index = 0;
        }
        return c;
    }

}
